import 'package:flutter/material.dart';
import 'package:jm_flutter/jm_flutter.dart';

import '../ui.dart';

class MyBottomNavBar extends StatefulWidget {
  final List<String> icons;
  final List<String> texts;
  final PageController? pageController;

  //final int? initIndex;
  final BottomNavigationBarType type;
  final Color? backgroundColor;
  final Color? activeColor;
  final Color? inactiveColor;
  final bool? svgIcon;
  final double iconSize;
  final double textSize;
  final FontWeight? textWeight;
  final double? elevation;
  final double? gap;
  final bool keepIconColor;
  final ValueChanged<int>? onTap;
  final BottomNavigationBarItem? centerItem;

  const MyBottomNavBar({
    Key? key,
    required this.icons,
    required this.texts,
    this.backgroundColor,
    this.svgIcon,
    this.onTap,
    this.activeColor,
    this.inactiveColor,
    this.iconSize = 26,
    this.textSize = 12,
    this.type = BottomNavigationBarType.fixed,
    this.elevation,
    this.pageController,
    this.keepIconColor = false,
    this.textWeight,
    this.gap,
    this.centerItem,
  }) : super(key: key);

  @override
  State<MyBottomNavBar> createState() => _MyBottomNavBarState();
}

class _MyBottomNavBarState extends State<MyBottomNavBar> {
  int activeIndex = 0;

  void handlePageChange() {
    if (!mounted) {
      return;
    }
    final index = widget.pageController!.page?.round() ?? 0;
    if (index != activeIndex) {
      setState(() {
        activeIndex = index;
      });
    }
  }

  @override
  initState() {
    super.initState();
    activeIndex = widget.pageController != null
        ? (widget.pageController?.initialPage ?? 0)
        : 0;

    if (widget.pageController != null) {
      widget.pageController!.addListener(handlePageChange);
    }
  }

  @override
  void dispose() {
    widget.pageController?.removeListener(handlePageChange);
    // TODO: implement dispose
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return BottomNavigationBar(
      type: widget.type,
      enableFeedback: true,
      backgroundColor: widget.backgroundColor,
      selectedFontSize: widget.textSize,
      selectedLabelStyle:
          TextStyle(fontWeight: widget.textWeight ?? FontWeight.normal),
      unselectedLabelStyle:
          TextStyle(fontWeight: widget.textWeight ?? FontWeight.normal),
      unselectedFontSize: widget.textSize,
      items: List.generate(widget.icons.length, (index) {
        final isActive = index == activeIndex;
        final color = isActive
            ? widget.activeColor ?? JmColors.main
            : widget.inactiveColor ?? Colors.grey[500];

        if (index == (widget.icons.length ~/ 2) && widget.centerItem != null) {
          return widget.centerItem!;
        }

        return BottomNavigationBarItem(
            icon: MyIcon(widget.icons[index],
                height: widget.iconSize,
                color: widget.keepIconColor ? null : color,
                svgIcon: widget.svgIcon),
            label: widget.texts[index]);
      }),
      currentIndex: activeIndex,
      selectedItemColor: widget.activeColor ?? JmColors.main,
      unselectedItemColor: widget.inactiveColor,
      elevation: widget.elevation,
      onTap: (index) {
        widget.pageController?.jumpToPage(index);
        widget.onTap?.call(index);
        // setState(() {
        //   activeIndex = index;
        // });
      },
    );
  }
}
